<?php
class SiteAdmin_CityController extends SiteAdminController
{
		
   	public function indexAction()
    {
    	$region = $this->getRequest()->getParam('region', 0);
    	$ar = Obj_Country::getCountries();
    	$countries = array();
    	$countries[0] = _('Выберите страну ...');
    	foreach ($ar as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	} 
    	$this->view->countries = $countries;
    	$arRegion = Obj_Region::getRegion($region);
    	$country = $this->getRequest()->getParam('country', false);
    	
    	if ($country === false && $arRegion !== false) $country = $arRegion['CountryID'];
    	$this->view->country = (int) $country;
    	
    	$ar = Obj_Region::getRegions($country);
    	$regions = array();
    	$regions[0] = _('Выберите регион ...');
    	foreach ($ar as $v) {
    		$regions[$v['ID']] = $v['Name'];
    	}
    	$this->view->RegionPage = Obj_LocationContentPage::getLocationContentPages($region, CONTENT_TYPE_REGION);
    	$this->view->region = $region;
    	$this->view->regions = $regions;
    	$this->view->cities = Obj_City::getCities($region);  
    }
    
   	public function editAction()
    {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
    	$arCity = Obj_City::getCity($id);
    	if ($addnew || $arCity === false) {
    		$this->view->city = array(
    			'ID' => 0,
    			'CountryID' => 0,
    			'RegionID' => 0,
    			'Name' => '',
    			'alias' => '',
    			'active' => 0,
    			'seoTitle' => '',
    			'seoDescription' => '',
    			'seoKeywords' => '',
    		);
    	}
    	else {
    		$this->view->city = $arCity;
    	}
    	
    	$ar = Obj_Country::getCountries();
    	$countries = array();
    	foreach ($ar as $v) {
    		$countries[$v['ID']] = $v['Name'];
    	} 
    	$this->view->countries = $countries;
    	$ar = Obj_Region::getRegions(reset(array_keys($countries)));
    	$regions = array();
    	foreach ($ar as $v) {
    		$regions[$v['ID']] = $v['Name'];
    	} 
    	$this->view->regions = $regions;
    }
    
    public function reloadRegionsAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arRegions = Obj_Region::getRegions($id);
    	$regions = array();
    	if ($arRegions !== false) foreach ($arRegions as $v) $regions[$v['ID']] = $v['Name'];
    	$this->_helper->json(array('error' => 0, 'regions' => $regions), true, false);
    }
    	
    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arCity = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
    	$arCity['Name'] = trim($this->getRequest()->getParam('Name', ''));
    	if ($arCity['Name'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь название!')), true, false);
    	}
    	$arCity['CountryID'] = intval($this->getRequest()->getParam('CountryID', ''));
    	if ($arCity['CountryID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь страну!')), true, false);
    	}
    	$arCity['RegionID'] = intval($this->getRequest()->getParam('RegionID', ''));
    	if ($arCity['RegionID'] === 0) {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Город должен иметь регион!')), true, false);
    	}
    	$arCity['alias'] = Obj_Text::translit($arCity['Name']);
    	$arCity['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
    	$arCity['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
    	$arCity['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
    	$arCity['pageText'] = trim(Obj_Text::removeHTMLComments($this->getRequest()->getParam('pageText', '')));
    	$arCity['active'] = intval($this->getRequest()->getParam('active', 0));
    	
    	$cid = Obj_City::saveCity($arCity, $id);
    	if ($arCity['active'] === 1) Obj_System_SearchIndex::write($cid, CONTENT_TYPE_CITY, $arCity['Name']);
    	else Obj_System_SearchIndex::delete($cid, CONTENT_TYPE_CITY);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index', 'region' => $arCity['RegionID']))), true, false);
    }
    
    public function deleteAction() {
        if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	Obj_City::delCity($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_CITY);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);  
    }
    
}


